package com.zfj.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zfj.entity.EduVideo;
import com.zfj.service.EduVideoService;
import com.zfj.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author zfj
 * @since 2022-10-07
 */
@RestController
@RequestMapping("/eduService/edu-video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    // 添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo video){

        boolean result = videoService.save(video);
        if (result){
            return R.ok();
        }
        return R.error();
    }

    // 删除小节
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") String id){

        // 根据小节id获取视频id;
//        EduVideo video = videoService.getById(id);
//        String videoSourceId = video.getVideoSourceId();

        boolean result = videoService.removeById(id);
        if (result){
            // 删除视频, 判断视频id是否为空
//            if (ObjectUtils.isNotEmpty(videoSourceId)){
//                R R = vodClient.removeAliyunVideo(video.getVideoSourceId());
//                // 触发熔断
//                if (!R.getSuccess()){
//                    return R.error();
//                }
//            }
            return R.ok();
        }
        return R.error();
    }

    // 根据id查询小节
    @GetMapping("/getVideo/{id}")
    public R getVideo(@PathVariable("id") String id){

        EduVideo video = videoService.getById(id);

        return R.ok().data("video", video);
    }

    // 修改小节
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo video){

        boolean result = videoService.updateById(video);

        if (result){
            return R.ok();
        }
        return R.error();
    }


}

